/*
 * Main.fx
 *
 * Created on 01 Agu 09, 17:41:41
 */

package gtranslatefx;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.control.Button;
import javafx.ext.swing.SwingComponent;
import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javafx.util.Sequences;

/**
 * @author Muhammad Hakim A
 */
var translatedText = "bonjour le monde";
var langform:String = Languages.ref[Sequences.indexOf(Languages.names, "FRENCH")];
var langto:String = Languages.ref[Sequences.indexOf(Languages.names, "ENGLISH")];
var jtextFrom = new JTextArea();
var jscrollFrom = new JScrollPane(jtextFrom);
var text: SwingComponent = SwingComponent.wrap(jscrollFrom);

public var jtextTo = new JTextArea();
var jscrollTo = new JScrollPane(jtextTo);
var textResult: SwingComponent = SwingComponent.wrap(jscrollTo);

var Translate = Translate{};
function translate ():Void{
    try {
       Translate.translate(jtextFrom.getText(), langform, langto);
       //println("result: {translatedText}");
       //jtextTo.setText(translatedText);
    } catch (e) {

    }
}

def btnTranslate = Button {
    layoutX: 10
    layoutY: 110
    text: "Translate"
    action: function() {
        translate();
    }
}

function copyResult ():Void{
    jtextTo.selectAll();
    jtextTo.copy();
}

def btnCopy = Button {
    layoutX: 100
    layoutY: 110
        text: "Copy"
        action: function() {
            copyResult();
        }
    }

def cmbFrom =  SwingComboBox {
        layoutX: 0
        layoutY: 0
        items: [ for (name in Languages.names)
            SwingComboBoxItem {
                text: name
            }
        ]
    }

def cmbTo = SwingComboBox {
        layoutX: 100
        layoutY: 0
        items: [ for (name in Languages.names)
            SwingComboBoxItem {
                text: name
            }
        ]
    }

function initValue () {
    text.width = 200;
    text.height = 100;
    text.layoutY = 50;
    textResult.width = 200;
    textResult.height = 100;
    textResult.layoutY = 200;
    jtextFrom.setLineWrap(true);
    jtextFrom.setWrapStyleWord(true);
    jtextTo.setLineWrap(true);
    jtextTo.setWrapStyleWord(true);
    jscrollFrom.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    jscrollTo.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//    jscrollFrom.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
//    jscrollTo.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

}

def stage = Stage {
    title: "TranlateFx"
    width: 300
    height: 300
    scene: Scene {
        content: [
            cmbFrom, cmbTo,
            text,
            btnTranslate, btnCopy,
            textResult
        ]
    }
}

public function run () {
    initValue();
    stage.visible = true;
}